<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}" media="all">

    <script th:src="@{/static/layui/layui.js}" charset="utf-8"></script>

</head>
<body>

<script type="text/javascript">
    var btnList = ['layui-btn-warm','layui-btn-danger'];
    function dict(obj,types) {
        var str = "";
        var index = 0;
        if(types === "state" ){
            if(obj.state == 0){//删除
                index = 1;
                str = "删除";
            }
            if(obj.state == 1){//未删除
                index = 0;
                str = "未删除";
            }
        }
        if(types === "typees" ){
            if(obj.state == 0){//文章
                index = 1;
                str = "文章";
            }
            if(obj.state == 1){//回答
                index = 0;
                str = "回答";
            }
        }
        var btn = "<a class='layui-btn layui-btn-xs layui-btn-radius "+btnList[index]+"'>"+str+"</a>";
        console.log("btn");
        console.log(btn);
        return btn;
    }

</script>

<div class="layui-form searchTable">
        <span>
            <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="text" name="discuss" id="discuss" placeholder="序号" autocomplete="off" class="layui-input datePicker">
            </div>
            </div>
            <button id="searchBtn" class="layui-btn" data-type="reload">查询</button>
        </span>
</div>

<script type="text/html" id="operation">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete" title="删除">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit" title="编辑">编辑</a>
</script>



<table class="layui-hide" id="questionTable" lay-filter="question"></table>

<script>
    layui.use(['jquery','table','form','laydate'],function () {
        var table = layui.table;//表单
        var layer = layui.layer;
        var $ = layui.$;
        var laydate = layui.laydate;

        table.render({
            elem:'#questionTable',
            url:'/discuss-queryAll',
            method: 'post',
           // toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
            defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            title: 'SysUser',
            cols: [
                [
                    {type: 'checkbox', fixed: 'left',width:50,align:'center'},
                    {field: 'id', title:'序号', align:'center',width:100},

                    {field: 'fid', title:'父id', align:'center', width:100},

                    {field: 'nid', title:'回答id/文章id', align:'center', width:100},

                    {field: 'uid', title:'用户id', align:'center', width:100},

                    {field: 'content', title:'评论内容', align:'center', width:100},

                    {field: 'state', title:'状态', align:'center', templet:function (data) {return dict(data,"state");}, width:100},

                    {field: 'likes', title:'点赞数',width:100, align:'center'},

                    {field: 'tread', title:'踩次数',width:100, align:'center'},

                    {field: 'reply', title:'回复次数',width:100,align:'center'},

                    {field: 'dates', title:'评论时间',width:250, align:'center'},

                    {field: 'createdate', title:'创建时间',width:250, align:'center'},

                    {field: 'updatedate', title:'更新时间',width:250, align:'center'},

                    {field: 'typees', title:'类型', align:'center', templet:function (data) {return dict(data,"typees");}, width:100},

                    {fixed: 'right', title:'操作', toolbar: '#operation', width:150, align:'center'}

                ]
            ],
            page: true,//每页条数默认10
            request: {
                pageName :"currentPage",
                limitName : "pageSize"
            },
            parseData: function (res) {//res 即为原始返回的数据
                console.log(res);
                return {
                    "code": 0, //解析接口状态
                    "msg": "", //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": res.list //解析数据列表
                };
            }

        });

        //content模糊查询
        var $  = layui.$;
        var active = {
            reload : function () {
                var content = $("#content").val();
                var obj = {};
                if(content != ""){
                    obj['content'] = content;
                }
                if(content == "" || content == null){
                    layer.msg("请输入值");
                    return;
                }
                /* var checkNum =/^[0-9]*$/;
                    if(!checkNum.test(vague)){
                        layer.msg("请输入序号");
                        return;
                }*/
                table.reload('questionTable',{
                    page: {
                        curr : 1
                    },
                    url: 'discuss-likeQuery/'+content,
                    method : 'POST',
                    where : obj,
                    done : function (res,curr,count) {
                        this.where  = {};
                    }
                },'data');
            }
        };

        $("#searchBtn").on("click",function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : "";
        });

        //状态查询
        /*       $("#stateBtn").on("click",function () {
            var state = $("#sysState").val();
            if(state == '' || state == null){
                layer.mag("请选择状态");
                return ;
            }
            var obj = {
                state: state
            };
            table.reload('SysUser',{
                page: {
                    curr : 1
                },
                url: '/sys/user-state/'+state,
                method : 'POST',
                where : obj,
                done : function (res,curr,count) {
                    this.where  = {};
                }
            },'data');
        });

        //新增
        table.on("toolbar(SysUser)",function (object) {
            switch(object.event){
                case "add" ://表格头部事件为新增
                {
                    var index = layer.open({
                        type:2,
                        area :['700px','450px'],
                        fixed: false,
                        maxmin: true,
                        content : '/sys/add'
                    });
                    layer.full(index);
                }

                    break;

                case 'LAYTABLE_TIPS' :
                    layer.alert('这是一个自定义的图标');
                    break;
            }
        });*/

        //编辑 or 删除事件
        table.on("tool(question)",function (object) {
            var data = object.data;//获取到你点击的哪一行的所有数据
            if(object.event === "edit"){//编辑
                //iframe层-父子操作
                var editIndex = layer.open({
                    type:2,//如果要打开一个html页面的话，这个type值就是2.如果要打开一个文本，这个值就是1.
                    area :['700px','450px'],
                    fixed: false,
                    maxmin: true,
                    content : 'idQuery/'+data.id
                });

                layer.full(editIndex);
            }
            else if(object.event === "delete"){//删除
                layer.confirm('删除只是将状态改为删除，确认要删除该评论吗？', function(index){
                    if(data.state == 0){
                        layer.msg("已经是删除状态,无法删除");
                        return;
                    }
                    //object.del();
                    var obj = {
                        id: data.id,
                        state: 0
                    };
                    $.post("discuss-delete",obj,function (data) {
                        if(data.success){
                            layer.msg("删除成功",{
                                icon: 1,
                                time: 1000
                            });

                        }else{
                            layer.msg("删除失败",{
                                icon: 1,
                                time: 2000
                            });
                        }
                        location.reload();
                    },"json");
                    layer.close(index);
                });
            }
        })
    })
</script>




</body>
</html>